﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MxWeiXinPF.Web.UI;
using System.Web.UI.HtmlControls;
using MxWeiXinPF.Common;
using System.Text;
using Senparc.Weixin.MP.CommonAPIs;
using Senparc.Weixin.Entities;

namespace MxWeiXinPF.Web.admin.users
{
    public partial class prescription_list : ManagePage
    {
        protected MxWeiXinPF.Model.manager admin_info = null;

        protected string keywords = string.Empty;

        protected int shopid = 0;
        protected string status = string.Empty;

        protected int wid = 0;

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            base.ChkAdminLevel("prescription_list", MXEnums.ActionEnum.Delete.ToString());
            int num = 0;
            int num2 = 0;
            MxWeiXinPF.BLL.userbbs userbbs = new MxWeiXinPF.BLL.userbbs();
            for (int i = 0; i < this.rptList.Items.Count; i++)
            {
                int id = Convert.ToInt32(((HiddenField)this.rptList.Items[i].FindControl("hidId")).Value);
                CheckBox box = (CheckBox)this.rptList.Items[i].FindControl("chkId");
                if (box.Checked)
                {
                    if (userbbs.Delete(id))
                    {
                        num++;
                    }
                    else
                    {
                        num2++;
                    }
                }
            }
            base.AddAdminLog(MXEnums.ActionEnum.Delete.ToString(), string.Concat(new object[] { "删除处方记录成功", num, "条，失败", num2, "条" }));
            base.JscriptMsg(string.Concat(new object[] { "删除成功", num, "条，失败", num2, "条！" }), Utils.CombUrlTxt("prescription_list.aspx", "keywords={0}", new string[] { this.txtKeywords.Text.Trim() }), "Success");
        }

        protected void btnSearch_Click2(object sender, EventArgs e)
        {
            base.Response.Redirect(Utils.CombUrlTxt("prescription_list.aspx", "keywords={0}&status={1}", new string[] { this.txtKeywords.Text.Trim(), this.ddlStatus.SelectedValue }));
        }

        protected string CombSqlTxt(int shopid, string _keywords, string status)
        {
            StringBuilder builder = new StringBuilder();
            _keywords = _keywords.Replace("'", "");
            if (!string.IsNullOrEmpty(_keywords))
            {
                builder.Append(" and title like '%" + _keywords + "%'");
            }
            if (shopid == 0)
            {
                this.admin_info = base.GetAdminInfo();
                shopid = this.admin_info.id;
            }
            if (!string.IsNullOrEmpty(status))
            {
                builder.Append(" and status =" + status);
            }
            builder.Append(" and (shopid=" + shopid + ")");
            return builder.ToString();
        }

        protected void ddlStatus_SelectedIndexChanged2(object sender, EventArgs e)
        {
            base.Response.Redirect(Utils.CombUrlTxt("prescription_list.aspx", "keywords={0}&status={1}", new string[] { this.txtKeywords.Text.Trim(), this.ddlStatus.SelectedValue }));
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            this.keywords = MXRequest.GetQueryString("keywords");
            this.status = MXRequest.GetQueryString("status");
            MxWeiXinPF.Model.manager weiXinCode = base.GetAdminInfo();
            this.wid = weiXinCode.id;
            this.admin_info = base.GetAdminInfo();
            this.shopid = this.admin_info.id;
            if (!this.Page.IsPostBack)
            {
                base.ChkAdminLevel("prescription_list", MXEnums.ActionEnum.View.ToString());
                this.RptBind(this.status, "id>0" + this.CombSqlTxt(this.shopid, this.keywords, this.status));
            }
        }

        private void RptBind(string status, string _strWhere)
        {
            this.txtKeywords.Text = this.keywords;
            if (!this.status.Equals(""))
            {
                this.ddlStatus.SelectedValue = status;
            }
            this.rptList.DataSource = new MxWeiXinPF.BLL.addprescription().GetListByView(0, _strWhere, "createtime desc");
            this.rptList.DataBind();
        }

        protected void rptList_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            string id = e.CommandArgument.ToString();
            MxWeiXinPF.BLL.addprescription addprescription = new MxWeiXinPF.BLL.addprescription();
            if (e.CommandName == "audit_pass")
            {
                if (addprescription.Audit(id, "1"))
                {
                    SendTemplateMsg("", true, id);
                    base.JscriptMsg("审核成功！", Utils.CombUrlTxt("prescription_list.aspx", "keywords={0}&status={1}", new string[] { this.txtKeywords.Text.Trim(), this.ddlStatus.SelectedValue }), "Success");
                }
                else
                {
                    SendTemplateMsg("", false, id);
                    base.JscriptMsg("审核失败！", "back", "Error");
                }
            }
            else if (e.CommandName == "audit_refuse")
            {
                Model.addprescription ma = addprescription.GetModel(Convert.ToInt32(id));
                if (ma != null)
                {
                    ma.status = "2";
                    ma.audittime = DateTime.Now;
                    if (addprescription.Update(ma))
                    {
                        SendTemplateMsg("", false, id);
                        base.JscriptMsg("拒绝成功！", Utils.CombUrlTxt("prescription_list.aspx", "keywords={0}&status={1}", new string[] { this.txtKeywords.Text.Trim(), this.ddlStatus.SelectedValue }), "Success");
                    }
                    else
                    {
                        //SendTemplateMsg("", false, id);
                        base.JscriptMsg("拒绝失败！", "back", "Error");
                    }
                }

            }
        }
        private bool SendTemplateMsg(string content, bool status, string id)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(id))
                {
                    return false;
                }
                int nid = Convert.ToInt32(id);

                Model.addprescription pmodel = new BLL.addprescription().GetModel(nid);
                if (pmodel == null)
                {
                    return false;
                }

                Model.manager mmodel = base.GetAdminInfo();
                if (mmodel == null)
                {
                    return false;
                }
                if (status)
                {
                    content = string.Concat("你好,你提交的处方已经审核通过,赶紧去购物车看看吧");
                }
                else
                {
                    content = string.Concat("你好,你提交的处方已经经管理员审核后,很遗憾没有通过,快去重新上传吧");
                }

                Model.wx_userweixin weixin = new MxWeiXinPF.BLL.wx_userweixin().GetModel(39);
                if (weixin == null)
                {
                    return false;
                }
                if (!AccessTokenContainer.CheckRegistered(weixin.AppId))
                {
                    AccessTokenContainer.Register(weixin.AppId, weixin.AppSecret);
                }

                WxJsonResult ret = Senparc.Weixin.MP.AdvancedAPIs.CustomApi.SendText(weixin.AppId, pmodel.openid, content);
                if (ret.errcode == 0)
                {
                    return true;
                }
            }
            catch (Exception ex)
            {

            }

            return false;
        }

        public string GetStatus(string id)
        {
            Model.addprescription mm = new BLL.addprescription().GetModel(Convert.ToInt32(id));
            if (mm != null)
            {
                if (mm.status == "0")
                {
                    return "未审核";
                }
                else if (mm.status == "1")
                {
                    return "已审核";
                }
                else
                {
                    return "已拒绝";
                }
            }
            return "已审核";
        }
    }
}